package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.ArticleClass;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.Staff;
import com.api.shopapi.entity.dto.ArticleClassDTO;
import com.api.shopapi.service.ArticleClassService;
import com.api.shopapi.service.StaffService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * 文章分类
 * */

@RestController
@RequestMapping("ArticleClass")
public class ArticleClassController extends BaseController{


    @Autowired
    @Qualifier("ArticleClassServiceImplOne")
    private ArticleClassService articleClassService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    //添加
    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            String parentArticleClassCode = jsonObject.getString("parentArticleClassCode");
            String icon = jsonObject.getString("icon");
            int sort = jsonObject.getIntValue("sort");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ArticleClass/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);
            ArticleClass articleClass=new ArticleClass();
            articleClass.setArticleClassCode(UUID.randomUUID().toString());
            if (parentArticleClassCode!=null&&!parentArticleClassCode.equals("")){
                articleClass.setParentArticleClassCode(parentArticleClassCode);
            }else {
                articleClass.setParentArticleClassCode(emptyUUID);
            }
            articleClass.setName(name);
            articleClass.setIcon(icon);
            articleClass.setSort(sort);
            articleClass.setStatus(1);
            articleClass.setGovernmentCode(staff.getGovernmentCode());

            articleClassService.insert(articleClass);
            Object data = new Object[] { };
            return success(data);

        }catch (Exception e){
            insertError("/ArticleClass/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //修改
    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String articleClassCode = jsonObject.getString("articleClassCode");
            String parentArticleClassCode = jsonObject.getString("parentArticleClassCode");
            String name = jsonObject.getString("name");
            String icon = jsonObject.getString("icon");
            int sort = jsonObject.getIntValue("sort");
            int status = jsonObject.getIntValue("status");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ArticleClass/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            ArticleClass articleClass = articleClassService.getByArticleClassCode(articleClassCode);
            if (articleClass==null){
                return failed("该活动分类不存在");
            }

            articleClass.setName(name);
            articleClass.setIcon(icon);
            articleClass.setSort(sort);
            articleClass.setStatus(status);
            articleClass.setParentArticleClassCode(parentArticleClassCode);

            articleClassService.update(articleClass);
            Object data = new Object[] { };
            return success(data);

        }catch (Exception e){
            e.printStackTrace();
            insertError("/ArticleClass/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String name = jsonObject.getString("name");
            int pageIndex = jsonObject.getIntValue("pageIndex");
            int pageSize = jsonObject.getIntValue("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ArticleClass/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<ArticleClassDTO> classDTOPageInfo =
                    articleClassService.getPage(pageIndex, pageSize, name, staff.getGovernmentCode());

            return success(classDTOPageInfo);

        }catch (Exception e){
            insertError("/ArticleClass/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "List",method = RequestMethod.POST)
    public MyResult List(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/ArticleClass/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);
            List<ArticleClassDTO> articleClassDTOList = articleClassService.getListByGovernmentCode(staff.getGovernmentCode());

            return success(articleClassDTOList);
        }catch (Exception e){
            insertError("/ArticleClass/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


}
